<?php

    class Config_Controller extends Application {
        
        function setup()
        {
            $this->actions = array('index','view','modify','delete');
            $this->before_filter('is_logged_in');
            $this->before_filter('had_access');
            
            // subnav
			$this->assign('subnav',admin_subnav());

			use_model('pixel_config');
			$this->use_layout('admin_layout.php');
        }
        
        function has_access()
        {
            if (!has_access_to('Admin::Modify Config')) {
                flash('error','Sorry but you do not have access to modify site configuration');
                $this->redirect_to(array('controller'=>'admin/dashboard'));
            }
        }
        
        function index()
        {
            $pixel_config = new pixel_config();
            $this->assign('report',$pixel_config->get_paged_report());
        }

        function modify()
        {
            $pixel_config = new pixel_config();
            $this->assign_ref('pixel_config',$pixel_config);
    
            if ( isset($_GET['id']) )
            {        
                $id = (int) $_GET['id'];
                $pixel_config->load($id);
            }
            if ( $_POST )
            {
                $id = (int) $_POST['pixel_config']['id'];
                $pixel_config->load($id);
                if ( $pixel_config->update_fields($_POST['pixel_config']) )
                {
                    flash('notice','Item Saved');
                    $this->redirect_to('index');
                } else {
                    flash('error','Item could not be saved');
                }
            }
        }

        function view()
        {
            if ( isset($_GET['id']) )
            {
                $pixel_config = new pixel_config();
                $id           = (int) $_GET['id'];
                if ( $pixel_config->load($id) )
                {
                    $this->assign('fields',$pixel_config->fields);
                } else {
                    flash('error','Item could not be loaded');
                    $this->redirect_to('index');
                }
            } else {
                flash('error','Item could not be loaded');
                $this->redirect_to('index');
            }
        }

        function delete()
        {
            if ( isset($_POST['id']) ) {
                $pixel_config = new pixel_config();
                $id           = (int) $_POST['id'];
                if ( $pixel_config->load($id) )
                {
                    $pixel_config->delete();
                    flash('notice','Item Deleted');
                } else {
                    flash('error','Item could not be deleted');
                }
            }
            $this->redirect_to('index'); 
        }

        
    }

?>